package cn.chint.manage.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ShellController { 


	public Logger log = Logger.getLogger(getClass());
	
	@RequestMapping(value = "shutdown",  produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String shutdown(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException{
		
		Process process = null;
        List<String> processList = new ArrayList<String>();
        try {
        	String parentDir = request.getServletContext().getRealPath("/");
			log.info(parentDir);
			int  index  = parentDir.indexOf("webapps")  ; 
			parentDir = parentDir.substring(0,index);
			parentDir= "sh "+parentDir+"bin/shutdown.sh"  ;
			log.info(parentDir);
			
            process = Runtime.getRuntime().exec(parentDir);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
  
        for (String line : processList) {
            System.out.println(line);
        }
		return "ok";
	}

	

   

    
	
}





